package cn.zyl.demo.jsp.controller;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * JspIndexController类说明: 视图控制器
 *
 * @author z.y.l
 * @version v1.0
 * @date 2020-09-01
 */
@Controller
@RequestMapping("/")
public class JspIndexController {
    /** RequestResponseBodyMethodProcessor:则是用于处理方法中含有@ResponseBody注解，或类上含有@ResponseBody注解 */
    @RequestMapping("")
    @ResponseBody
    public String root(){
        return "hello tomcat jsp";
    }

    /** ViewNameMethodReturnValueHandler 处理返回值是String类型（前提不含@ResponseBody标签）*/
    @RequestMapping("index")
    public String index(){
        /*字符串以redirect:开始，不再作为view视图名而是作为重定向的地址。以forward：开头便是转发*/
        return "index";
    }

    /** HttpEntityMethodProcessor 处理返回值类型是HttpEntity的方法（文件下载）*/
    @RequestMapping(value="httpEntity",method= RequestMethod.GET)
    public HttpEntity<String> testHttpEntity() {
        String body="中国";
        HttpHeaders headers=new HttpHeaders();
        headers.add("Content-type","text/html;charset=GBK");
        return new HttpEntity<>(body, headers);
    }

    /** ModelMethodProcessor:用来处理返回类型为Model的，它默认采用请求路径作为视图名称*/
    @RequestMapping(value="model",method= RequestMethod.GET)
    public Model handleModel() throws Exception {
        Model model=new ExtendedModelMap();
        model.addAttribute("name","[请求路径作为视图名称]");
        return model;
    }

    /** ModelAndViewMethodReturnValueHandler：用来处理返回值类型为ModelAndView*/
    @RequestMapping("hello")
    public ModelAndView hello(HttpServletRequest request){
        //设置对应JSP的模板文件
        ModelAndView modelAndView = new ModelAndView("hello");
        //设置${info}标签的值
        modelAndView.addObject("info", "Hello,JSP.");
        //将用户信息保存到Request对象中
        request.setAttribute("name","测试jsp");
        return modelAndView;
    }
    /*
        handlers.add(new ModelAndViewMethodReturnValueHandler()); // 处理返回类型ModelAndView
        handlers.add(new ModelMethodProcessor()); // 处理返回类型Model
        handlers.add(new ViewMethodReturnValueHandler()); // 处理返回类型View
        handlers.add(new ResponseBodyEmitterReturnValueHandler(getMessageConverters())); // 处理返回类型 ？
        handlers.add(new StreamingResponseBodyReturnValueHandler()); // 处理返回类型StreamingResponseBody
        handlers.add(new HttpEntityMethodProcessor(getMessageConverters(), // 处理返回类型HttpEntity|RequestEntity
                this.contentNegotiationManager, this.requestResponseBodyAdvice));
        handlers.add(new HttpHeadersReturnValueHandler()); // 处理返回类型HttpHeaders
        handlers.add(new CallableMethodReturnValueHandler()); // 处理返回类型Callable
        handlers.add(new DeferredResultMethodReturnValueHandler()); // 处理返回类型DeferredRequest | ListenableFuture
        handlers.add(new AsyncTaskMethodReturnValueHandler(this.beanFactory)); // 处理返回类型WebAsyncTask
        // Annotation-based return value types
        handlers.add(new ModelAttributeMethodProcessor(false)); //处理@ModelAttribute
        handlers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(), // 处理@ResponseBody
                this.contentNegotiationManager, this.requestResponseBodyAdvice));
        // Multi-purpose return value types
        handlers.add(new ViewNameMethodReturnValueHandler()); // 处理返回类型Void.class | CharSequense.class
        handlers.add(new MapMethodProcessor()); // 处理返回类型Map
        if (getCustomReturnValueHandlers() != null) {
            handlers.addAll(getCustomReturnValueHandlers()); // 自定义
        }
        // Catch-all
        if (!CollectionUtils.isEmpty(getModelAndViewResolvers())) {
            handlers.add(new ModelAndViewResolverMethodReturnValueHandler(getModelAndViewResolvers()));
        }else {
            handlers.add(new ModelAttributeMethodProcessor(true)); // 处理返回类型ModelAttribute | 普通类型
        }
    */
}
